$(function () {
   layui.config({
       base: 'static/layuiadmin/' //
   }).extend({
       index: 'lib/index'
    }).use(['layer', 'form','index'],function () {
        var form=layui.form,
            layer=layui.layer,
            modeutils=layui.modeutils,
            $ = layui.$;
        //初始化学校列表
       $.ajax({
           url:'xqz/school_list.do'
           ,dataType:'json'
           ,type:'post'
           ,success(res){
               var data=res.data;
               for (var i=0;i<data.length;i++){
                       data[i].schoolCreateDt=timeStamp2String(data[i].schoolCreateDt);
                   var item=' <div class="layui-col-md4 layui-col-sm6">\n' +
                       '                <div class="layadmin-contact-box" >\n' +
                       '                    <div class="layui-col-md4 layui-col-sm6">\n' +
                       '                        <a class="sc-item" href="javascript:;" ondblclick="updateSchool('+data[i].schoolId+')">\n' +
                       '                            <div class="layadmin-text-center">\n' +
                       '                                <img src="static/layuiadmin/style/res/template/character.jpg">\n' +
                       '                                <div class="layadmin-maillist-img layadmin-font-blod">第'+data[i].schoolId+' 分校</div>\n' +
                       '                            </div>\n' +
                       '                        </a>\n' +
                       '                    </div>\n' +
                       '\n' +
                       '                    <div class="layui-col-md8 layadmin-padding-left20 layui-col-sm6">\n' +
                       '                        <a href="javascript:;">\n' +
                       '                            <h3 class="layadmin-title">\n' +
                       '                                <strong>负责人'+data[i].schoolMaster+'  </strong>\n' +
                       '                            </h3>\n' +
                       '                            <p class="layadmin-textimg">\n' +
                       '                                <i class="layui-icon layui-icon-location"></i>\n' +
                       '                                地址'+data[i].schoolAddr +'\n' +
                       '                            </p>\n' +
                       '                        </a>\n' +
                       '                        <div class="layadmin-address">\n' +
                       '                            <a href="javascript:;">\n' +
                       '                                <strong>Xin Qi Zhi</strong>\n' +
                       '                                <br>\n' +
                       '                                创建时间：' +data[i].schoolCreateDt+ '\n' +
                       '                                <br>\n' +
                       '                                运行状态：'+data[i].schoolStatus  +'\n' +
                       '                                <br>\n' +
                       '                                <addr title="phone">Tel:</addr>\n' +
                       '                                (+86) 18886035015 \n' +
                       '                            </a>\n' +
                       '                        </div>\n' +
                       '                    </div>\n' +
                       '                </div>\n' +
                       '      ';
                       $("#school-list").append(item);
               }
           }
       });
        //添加分校窗口
        form.on('submit(lay-btn-AddSchool)',function (data) {
            layer.open({
                type:1
                ,title:'添加分校'
                ,btn:['添加','取消']
                ,content: $('#layui-app-school-list')
                ,maxmin: true
                ,area: ['600px', '250px']
                ,yes:function (index,layro) {
                    //先校验工号是否存在正确
                    //pass
                    //pass
                   var scid=$('#schStaffId').val();
                   var scAdr=$('#schAddr').val();
                   console.log(scid+"--添加分校"+scAdr);
                    $.ajax({
                        url:'xqz/add_school.do'
                        ,dataType:'json'
                        ,type:'post'
                        ,data:{
                            schoolStaffId:scid,
                            schoolAddr:scAdr
                        },success:function (res) {
                            if (res.success){
                                layer.alert(res.msg,{icon: 1,title:'提示'},function () {
                                    layer.close(index);
                                });
                                window.location.reload();
                            }else {
                                layer.alert('操作失败',{icon: 2,title:'提示'},function () {
                                    layer.close(index);
                                })
                            }
                        }
                    });
                }
                ,btn2:function (index,layro) {
                    layer.close(index);
                }
            });
            return false;
        });
   })
});
//时间格式化
function timeStamp2String(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
}

/*图片双击事件修改分校信息*/
function updateSchool(sid) {
    //表单初始化
    $.ajax({
       url:'xqz/school.do'
        ,type:'post'
        ,dataType:'json'
        ,data: {
           'schId':sid
        }
        ,success:function (res) {
           if (res.success){
               var data=res.data;
               $("#schoolStaffId").val(data.schoolStaffId);
               $("#schoolId").val(data.schoolId);
              /* $("#schoolStatus").val(data.schoolStatus);*/
               $("#schoolAddr").val(data.schoolAddr);
           }
        }
    });
   /* */
    layer.open({
        type:1
        ,content: $('#update-school')
        ,btn: ['修改','取消','删除']
        ,maxmin: true
        ,area: ['800px','400px']
        ,yes:function (index,layro) {//yes按钮
            layer.close(index);
            //提交修改数据
            $.ajax({
                url:"xqz/update_school.do"
                ,type:'post'
                ,dataType:'json'
                ,data:{
                    'schoolId': $("#schoolId").val(),
                    'schoolStaffId': $("#schoolStaffId").val(),
                    'schoolStatus':$("#schoolStatus").val(),
                    'schoolAddr':$("#schoolAddr").val()
                }
                ,success:function (res) {
                    layer.alert(res.msg,function () {
                            window.location.reload();
                    });
                }
            });
        }
        ,btn2:function (index,layro) {//取消按钮
            layer.close(index);
        }
        ,btn3:function (index,layro) {//删除按钮
            layer.confirm('是否确定删除该分校？',{btn:['确定','取消']},function () {
                $.ajax({//确定按钮发送删除
                    url:'xqz/del_school.do'
                    ,type:'post'
                    ,dataType:'json'
                    ,data:{
                        'schId':sid
                    }
                    ,success:function (res) {
                        layer.alert(res.msg,{icon:1});
                        window.location.reload();
                    }
                });
            },function () {
                layer.close(index);
            })
        }
    });
}